package com.xuwl.controller;

import com.xuwl.domain.RedisModel;
import com.xuwl.service.RedisService;
import com.xuwl.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ┏┛ ┻━━━━━┛ ┻┓
 * ┃　　　━　　　┃
 * ┃　┳┛　  ┗┳　┃
 * ┃　　　　　　 ┃
 * ┃　　　┻　　　┃
 * ┗━┓　　　┏━━━┛
 * ┃　　　┃   神兽保佑,代码无BUG！
 * ┃　　　┗━━━━━━━━━┓
 * ┃　　　　　　　   ┣┓
 * ┃　　　　        ┏┛
 * ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
 * ┃ ┫ ┫   ┃ ┫ ┫
 * ┗━┻━┛   ┗━┻━┛
 * Created by xuwl on 2018/5/17.
 */
@RestController
@RequestMapping("/testController")
public class TestController {
    @Autowired
    private RedisService service;
    @Autowired
    RedisCacheUtil redisCacheUtil;
    //添加
    @GetMapping("/add")
    public void test() {
        System.out.println("start.....");
        RedisModel m = new RedisModel();
        m.setName("张三");
        m.setTel("1111");
        m.setAddress("深圳1");
        m.setRedisKey("zhangsanKey01");
//        redisCacheUtil.setCacheObject(m.getRedisKey(), m, 0);
        service.put(m.getRedisKey(), m, -1);
        RedisModel m2 = new RedisModel();
        m2.setName("张三2");
        m2.setTel("2222");
        m2.setAddress("深圳2");
        m2.setRedisKey("zhangsanKey02");
        service.put(m2.getRedisKey(), m2, -1);

        RedisModel m3 = new RedisModel();
        m3.setName("张三3");
        m3.setTel("2222");
        m3.setAddress("深圳2");
        m3.setRedisKey("zhangsanKey03");
        service.put(m3.getRedisKey(), m3, -1);

        System.out.println("add success end...");
    }

    //查询所有对象
    @GetMapping("/getAll")
    public Object getAll() {
        return service.getAll();
    }

    //查询所有key
    @GetMapping("/getKeys")
    public Object getKeys() {
        return service.getKeys();
    }

    //根据key查询
    @GetMapping("/get")
    public Object get() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey02");
        return service.get(m.getRedisKey());
    }

    //删除
    @GetMapping("/remove")
    public void remove() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        service.remove(m.getRedisKey());
    }

    //判断key是否存在
    @GetMapping("/isKeyExists")
    public void isKeyExists() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        boolean flag = service.isKeyExists(m.getRedisKey());
        System.out.println("zhangsanKey01 是否存在: "+flag);
    }

    //查询当前缓存的数量
    @GetMapping("/count")
    public Object count() {
        return service.count();
    }

    //清空所有key
    @GetMapping("/empty")
    public void empty() {
        service.empty();
    }

}
